package com.deer.wms.base.system.web.pda;

import com.deer.wms.base.system.model.pda.BarSelectModel;
import com.deer.wms.base.system.model.pda.BoxOverViewYRKModel;
import com.deer.wms.base.system.model.pda.BoxSelectBarModel;
import com.deer.wms.base.system.model.pda.PdaActionRecord;
import com.deer.wms.base.system.model.zh.AssessUserDeptModel;
import com.deer.wms.base.system.service.pda.PdaActionRecordService;
import com.deer.wms.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Decoder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//pda记录
@Controller
@RequestMapping("/pdaActionRecord")
public class PdaActionRecordController {
    @Autowired
    PdaActionRecordService pdaActionRecordService;



    //根据托号查晶棒信息
    @GetMapping("/findBoxBar")
    @ResponseBody
    public List<BoxSelectBarModel> findBoxBar(String torr){
        return pdaActionRecordService.findBoxBar(torr);
    }
    //查询已经出库的晶棒信息
    @GetMapping("/findAllBoxYCK")
    @ResponseBody
    public List<BoxOverViewYRKModel> findAllBoxYCK(int time) {
        return pdaActionRecordService.findAllBoxYCK(time);
    }
    //查询已经入库的晶棒信息
    @GetMapping("/findAllBoxYRK")
    @ResponseBody
    public List<BoxOverViewYRKModel> findAllBoxYRK(int time) {
        return pdaActionRecordService.findAllBoxYRK(time);
    }

    //查询晶棒信息
    @RequestMapping("/findBarInfo")
    @ResponseBody
    public BarSelectModel findBarInfo(String bar) {
        return pdaActionRecordService.findBarInfo(bar);
    }

    //查询当前用户部门
    @GetMapping("/assessUserDept")
    @ResponseBody
    public AssessUserDeptModel assessUserDept(String userId) {
        try {
            //解码
            BASE64Decoder decoder = new BASE64Decoder();

            byte[] d = decoder.decodeBuffer(userId);

            String str = new String(d);
            AssessUserDeptModel assessUserDept = pdaActionRecordService.assessUserDept(Long.parseLong(str));
            assessUserDept.setState("OK");
            return assessUserDept;
        } catch (IOException e) {
            AssessUserDeptModel assessUserDept = new AssessUserDeptModel();
            assessUserDept.setState("NG");
            return assessUserDept;
        }
    }

    //查询名称
    @RequestMapping("selectusername")
    @ResponseBody
    public String[] selectusername() {
        List<PdaActionRecord> strings = pdaActionRecordService.selectUsername();
        if (strings == null) {
            strings = new ArrayList<>();
            PdaActionRecord pdaActionRecord = new PdaActionRecord();
            pdaActionRecord.setAtUser("未获取到用户");
            strings.add(0, pdaActionRecord);
        } else {
            PdaActionRecord pdaActionRecord = new PdaActionRecord();
            pdaActionRecord.setAtUser("所有");
            strings.add(0, pdaActionRecord);
        }
        String[] array = new String[strings.size()];
        for (int i = 0; i < strings.size(); i++) {
            array[i] = strings.get(i).getAtUser();
        }
//        StringBuilder sb=new StringBuilder();
//        for (PdaActionRecord s:strings
//             ) {
//            String atUser = s.getAtUser();
//            sb.append(atUser+",");
//        }
//        String[] split = sb.toString().split(",");
        //String[] strs = strings.toArray(new String[strings.size()]);
        return array;
    }

    @RequestMapping("saverecord")
    @ResponseBody
    public String saverecord(PdaActionRecord pdaActionRecord) {
        String time = DateUtils.getTime();
        String substring = time.substring(0, 19);
        pdaActionRecord.setCreateTime(substring);
        pdaActionRecordService.save(pdaActionRecord);
        return "true";
    }

    @RequestMapping("selectrecord")
    @ResponseBody
    public List<PdaActionRecord> selectrecord(PdaActionRecord pdaActionRecord) {
        List<PdaActionRecord> selectday = new ArrayList<>();
        if (pdaActionRecord.getTimeState().equals("0")) {
            selectday = pdaActionRecordService.selectday(pdaActionRecord);
        } else if (pdaActionRecord.getTimeState().equals("1")) {
            selectday = pdaActionRecordService.selectweek(pdaActionRecord);
        } else if (pdaActionRecord.getTimeState().equals("2")) {
            selectday = pdaActionRecordService.selectmonth(pdaActionRecord);
        } else if (pdaActionRecord.getTimeState().equals("3")) {
            selectday = pdaActionRecordService.selectyear(pdaActionRecord);
        } else if (pdaActionRecord.getTimeState().equals("4")) {
            selectday = pdaActionRecordService.selectall(pdaActionRecord);
        }
        return selectday;
    }

}
